package listeners;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * 针对ServletContext对象的监听器，监听Context对象的生成/销毁，以及属性的添加/修改/删除
 *
 */
@WebListener
public class MyContextListener implements ServletContextListener, ServletContextAttributeListener {

    /**
     * Default constructor. 
     */
    public MyContextListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * 监听向Context对象中添加属性
     * Servlet：context.view中的第1次计数时将触发此方法
     */
    public void attributeAdded(ServletContextAttributeEvent scae)  { 
         // TODO Auto-generated method stub
    	System.out.println("向Context对象中添加了新的属性："+scae.getName()+",属性的值为："+scae.getValue());
    }

    /**
     * 监听从Context对象中删除属性
     */
    public void attributeRemoved(ServletContextAttributeEvent scae)  { 
         // TODO Auto-generated method stub
    }

    /**
     * 监听向Context对象的销毁
     */
    public void contextDestroyed(ServletContextEvent sce)  { 
         // TODO Auto-generated method stub
    	System.out.println("context is destroyed");
    }

    /**
     * 监听Context对象的属性值的修改
     * Servlet：context.view中的每次计数时修改count的值时将触发此方法
     */
    public void attributeReplaced(ServletContextAttributeEvent scae)  { 
         // TODO Auto-generated method stub
    	System.out.println("Context对象中："+scae.getName()+"属性的值被修改为："+scae.getValue());
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce)  { 
         // TODO Auto-generated method stub
    	System.out.println("Context对象被初始化");
    }
	
}
